
$(document).ready(function(){

	var pusher = new Pusher(PUSHER_APP_KEY);
	var channel = pusher.subscribe('my-channel');
	
	channel.bind('update-total-posts', function(data) {
		var user_id = G_USER_ID;
		if(user_id == data.sender_id)
		{
			$.ajax({
				url: URL + 'feeds/updateTotalPosts',
				data: {user_id:data.sender_id},
				type: 'post',
				success: function(total){
					$('#total_posts').text(total);
				}
			});
		}
	});

	channel.bind('request-sent', function(data) {
		var sender_id = data.sender_id;
		var receiver_id = data.receiver_id;
		if(receiver_id == SS_USER_ID)
		{
			getInfo(sender_id).done(function(result){
				
				if(result.status == true)
				{
					pendingContainer.prepend(addToPendingList(result.data));
					$('#errRequest').hide();
				}
			});
			
		}
	});

	channel.bind('accepted-request', function(data) {
		var sender_id = data.user_id;
		var receiver_id = data.friend_id;
		if(receiver_id == SS_USER_ID)
		{
			getInfo(sender_id).done(function(result){
				
				if(result.status == true)
				{
					$('.connectionList').prepend(addToConnectionList(result.data));
					$('#errRequest2').hide();
				}
			});
			
		}
	});


	updateMessage();
	updateMessage2();
});

var pendingTab = $('#tabPending');
var pendingContainer = $('.pendingList');
var template = $('#pendingTpl');

 /* pendingTab.on('click',function(){
	listPending().done(function(result){
		var content = '';
		$(result).each(function(i,val){
			content += addToPendingList(val);	
		});
		pendingContainer.html(content);
		if(content == '')
		{
			pendingContainer.html('<center> No pending request </center>');
		}
	});
}); */

function updateMessage()
{
	var requestList = $('.request_member').length;

	if(requestList == 0)
	{
		$('#errRequest').show();
	}
	else
	{
		$('#errRequest').hide();		
	}
	
}

function updateMessage2()
{
	var requestList = $('.connection_member').length;

	if(requestList == 0)
	{
		$('#errRequest2').show();
	}
	else
	{
		$('#errRequest2').hide();		
	}
}
function getInfo(user_id)
{
	params = {};
	params.user_id = user_id;
	return $.ajax({
		url: URL + 'api/getInfo',
		data:params,
		type: 'get',
		dataType: 'json'
	});
}

function listPending()
{
	return $.ajax({
		url: URL + 'profile/listPending',
		data:'',
		type: 'get',
		dataType: 'json'
	});
}


function addToPendingList(data)
{
	var template1 = template.html();
	var html = Mustache.to_html(template1,data);
	return html;
}

function addToConnectionList(data)
{
	var template1 = $('#connectionTpl').html();
	var html = Mustache.to_html(template1,data);
	return html;
}


function acceptRequest(sender_id,receiver_id)
{
	var params = {};
	params.sender_id = sender_id;
	params.receiver_id = receiver_id;

	return $.ajax({
		url: URL + 'connections/acceptRequest',
		data: params,
		type: 'post',
		dataType: 'json'
	});
}

$(document).one('click','.btnAcceptPending',function(){
	var sender_id = $(this).attr('data-sender-id');
	var receiver_id = G_USER_ID;

	acceptRequest(sender_id,receiver_id).done(function(result){
		if(result.status == true)
		{
			$('#sender_id_'+sender_id).fadeOut(300,function(){$(this).remove(); updateMessage();});
			
		}
	});
});